import { Component, OnInit } from '@angular/core';
import { Router,NavigationExtras } from '@angular/router';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { DefaultLanguageService } from '@services/default-language.service';
import {TranslateService} from '@ngx-translate/core';
import { CommonResponse,VerificationType,ToastColor,CommonService,AppVerificationCode,AppParam } from '@services/common-service.service';

@Component({
  selector: 'app-email-create',
  templateUrl: './email-create.page.html',
  styleUrls: ['./email-create.page.scss'],
})
export class EmailCreatePage implements OnInit {

  public i18n:any = {};

  public validateForm:FormGroup;

  constructor(
    private router: Router,
    private defaultLanguageService:DefaultLanguageService,
    private translate:TranslateService,
    private commonService:CommonService) { }

  ngOnInit() {
    this.initLanguage();
    this.validateForm = new FormGroup({
      'email': new FormControl('', [
        Validators.required,
        Validators.pattern(/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/)
      ])
    })
  }

  initLanguage(){
    this.i18n = this.commonService.initTranslate([]);
  }

  get email(){return this.validateForm.get('email');}

  verifyEmail(){
    if(this.validateForm.invalid){
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
      return false;
    }
    let navigationExtras: NavigationExtras = {
      queryParams: { 'email': this.validateForm.value.email }
    };
    this.router.navigate(['/tabs/myAccount/setting/account-security/email-verify'],navigationExtras);
  }

}
